/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.parent.web;


import com.fanrenqiji.common.config.GlobalSetting;
import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.web.BaseController;
import com.fanrenqiji.modules.business.parent.vo.StudentVO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.fanrenqiji.modules.business.parent.vo.ParentVO;
import com.fanrenqiji.modules.business.parent.service.ParentService;

@Controller
@RequestMapping(value = "${adminPath}/business/parent")
public class ParentController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(ParentController.class);
    @Autowired
    private ParentService parentService;

    @ModelAttribute("parent")
    public ParentVO getParent(@RequestParam(required = false) String id) {
        if (StringUtils.isNotEmpty(id)) {
            return parentService.get(id);
        } else {
            return new ParentVO();
        }
    }

    /**
     * 列表
     *
     * @return
     */
    @RequiresPermissions("business:parent:view")
    @RequestMapping(value = {"list", ""})
    public String list(ParentVO parent, HttpServletRequest request,
                       HttpServletResponse response, Model model) {
        Page<ParentVO> page = parentService.findParentByPage(new Page<ParentVO>(request, response),
                parent);
        model.addAttribute("page", page);
        return "modules/business/parent/parentList";
    }

    /**
     * 用户添加更新表
     *
     * @param model
     * @return
     */
    @RequiresPermissions("business:parent:view")
    @RequestMapping(value = "form")
    public String form(Model model, ParentVO parent) {
        if (StringUtils.isNotEmpty(parent.getId())) {
            model.addAttribute("parent", parentService.get(parent.getId()));
        } else {
            model.addAttribute("parent", parent);
        }
        return "modules/business/parent/parentForm";
    }

    /**
     * 添加或修改提交
     *
     * @param parent
     * @param model
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("business:parent:edit")
    @RequestMapping(value = "save")
    public String save(ParentVO parent, Model model,
                       RedirectAttributes redirectAttributes) {// TODO @Valid 添加后端校验
        if (!beanValidator(model, parent)) {
            return form(model, parent);
        }
        parentService.update(parent);
        addMessage(redirectAttributes, "保存成功");
        return "redirect:" + GlobalSetting.getAdminPath()
                + "/business/parent/?repage";
    }

    /**
     * 删除操作
     *
     * @return
     */
    @RequestMapping("delete")
    @RequiresPermissions("business:parent:edit")
    public String delete(String id, RedirectAttributes redirectAttributes) {
        parentService.delete(id);
        addMessage(redirectAttributes, "删除成功");
        return "redirect:" + GlobalSetting.getAdminPath() + "/business/parent/";
    }

    /**
     * 放大镜 家长 All
     */
    @ResponseBody
    @RequestMapping(value = "all")
    public List<Map<String, Object>> all(HttpServletResponse response) {
        response.setContentType("application/json; charset=UTF-8");
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<ParentVO> list = parentService.getAll();
        for (int i = 0; i < list.size(); i++) {
            ParentVO vo = list.get(i);
            if (vo != null) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("name", vo.getName());
                map.put("id", vo.getId());
                mapList.add(map);
            }
        }
        return mapList;
    }
}